﻿namespace Hidistro.ControlPanel.Store
{
    using Hidistro.Core;
    using Hidistro.Core.Enums;
    using Hidistro.Entities;
    using Hidistro.Entities.Store;
    using Hidistro.Entities.VShop;
    using Hidistro.Membership.Context;
    using Hidistro.SqlDal;
    using Hidistro.SqlDal.Store;
    using Hidistro.SqlDal.VShop;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.IO;
    using System.Web;
    using System.Xml;

    public static class StoreHelper
    {
        public static void AddHotkeywords(int categoryId, string keywords)
        {
            new HotkeywordDao().AddHotkeywords(categoryId, keywords);
        }

        public static string BackupData()
        {
            return new BackupRestoreDao().BackupData(HttpContext.Current.Request.MapPath(Globals.ApplicationPath + "/storage/data/Backup/"));
        }

        public static bool CanAddMenu(int parentId, ClientType clientType)
        {
            IList<MenuInfo> menusByParentId = new MenuDao().GetMenusByParentId(parentId, clientType);
            if ((menusByParentId == null) || (menusByParentId.Count == 0))
            {
                return true;
            }
            if (parentId == 0)
            {
                return (menusByParentId.Count < 3);
            }
            return (menusByParentId.Count < 5);
        }

        public static bool CreateFriendlyLink(FriendlyLinksInfo friendlyLink)
        {
            if (null == friendlyLink)
            {
                return false;
            }
            return new FriendlyLinkDao().CreateUpdateDeleteFriendlyLink(friendlyLink, DataProviderAction.Create);
        }

        public static int CreateVote(VoteInfo vote)
        {
            int num = 0;
            VoteDao dao = new VoteDao();
            long num2 = dao.CreateVote(vote);
            if (num2 > 0L)
            {
                num = 1;
                if (vote.VoteItems == null)
                {
                    return num;
                }
                foreach (VoteItemInfo info in vote.VoteItems)
                {
                    info.VoteId = num2;
                    info.ItemCount = 0;
                    num += dao.CreateVoteItem(info, null);
                }
            }
            return num;
        }

        public static bool DeleteBackupFile(string backupName)
        {
            string filename = HiContext.Current.Context.Request.MapPath(Globals.ApplicationPath + "/config/BackupFiles.config");
            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                XmlNodeList childNodes = document.SelectSingleNode("root").ChildNodes;
                foreach (XmlNode node in childNodes)
                {
                    XmlElement element = (XmlElement) node;
                    if (element.GetAttribute("BackupName") == backupName)
                    {
                        document.SelectSingleNode("root").RemoveChild(node);
                    }
                }
                document.Save(filename);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void DeleteHotKeywords(int hid)
        {
            new HotkeywordDao().DeleteHotKeywords(hid);
        }

        public static void DeleteImage(string imageUrl)
        {
            if (!string.IsNullOrEmpty(imageUrl))
            {
                try
                {
                    string path = HiContext.Current.Context.Request.MapPath(Globals.ApplicationPath + imageUrl);
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
                catch
                {
                }
            }
        }

        public static bool DeleteMenu(int menuId)
        {
            return new MenuDao().DeleteMenu(menuId);
        }

        public static int DeleteVote(long voteId)
        {
            return new VoteDao().DeleteVote(voteId);
        }

        public static int FriendlyLinkDelete(int linkId)
        {
            return new FriendlyLinkDao().FriendlyLinkDelete(linkId);
        }

        public static DataTable GetBackupFiles()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BackupName", typeof(string));
            table.Columns.Add("Version", typeof(string));
            table.Columns.Add("FileSize", typeof(string));
            table.Columns.Add("BackupTime", typeof(string));
            string filename = HiContext.Current.Context.Request.MapPath(Globals.ApplicationPath + "/config/BackupFiles.config");
            XmlDocument document = new XmlDocument();
            document.Load(filename);
            XmlNodeList childNodes = document.SelectSingleNode("root").ChildNodes;
            foreach (XmlNode node in childNodes)
            {
                XmlElement element = (XmlElement) node;
                DataRow row = table.NewRow();
                row["BackupName"] = element.GetAttribute("BackupName");
                row["Version"] = element.GetAttribute("Version");
                row["FileSize"] = element.GetAttribute("FileSize");
                row["BackupTime"] = element.GetAttribute("BackupTime");
                table.Rows.Add(row);
            }
            return table;
        }

        public static FriendlyLinksInfo GetFriendlyLink(int linkId)
        {
            return new FriendlyLinkDao().GetFriendlyLink(linkId);
        }

        public static IList<FriendlyLinksInfo> GetFriendlyLinks()
        {
            return new FriendlyLinkDao().GetFriendlyLinks();
        }

        public static string GetHotkeyword(int id)
        {
            return new HotkeywordDao().GetHotkeyword(id);
        }

        public static DataTable GetHotKeywords()
        {
            return new HotkeywordDao().GetHotKeywords();
        }

        public static IList<MenuInfo> GetInitMenus(ClientType clientType)
        {
            MenuDao dao = new MenuDao();
            IList<MenuInfo> topMenus = dao.GetTopMenus(clientType);
            foreach (MenuInfo info in topMenus)
            {
                info.Chilren = dao.GetMenusByParentId(info.MenuId, clientType);
                if (info.Chilren == null)
                {
                    info.Chilren = new List<MenuInfo>();
                }
            }
            return topMenus;
        }

        public static MenuInfo GetMenu(int menuId)
        {
            return new MenuDao().GetMenu(menuId);
        }

        public static IList<MenuInfo> GetMenus(ClientType clientType)
        {
            IList<MenuInfo> list = new List<MenuInfo>();
            MenuDao dao = new MenuDao();
            IList<MenuInfo> topMenus = dao.GetTopMenus(clientType);
            if (topMenus != null)
            {
                foreach (MenuInfo info in topMenus)
                {
                    list.Add(info);
                    IList<MenuInfo> menusByParentId = dao.GetMenusByParentId(info.MenuId, clientType);
                    if (menusByParentId != null)
                    {
                        foreach (MenuInfo info2 in menusByParentId)
                        {
                            list.Add(info2);
                        }
                    }
                }
            }
            return list;
        }

        public static IList<MenuInfo> GetMenusByParentId(int parentId, ClientType clientType)
        {
            return new MenuDao().GetMenusByParentId(parentId, clientType);
        }

        public static IList<MenuInfo> GetTopMenus(ClientType clientType)
        {
            return new MenuDao().GetTopMenus(clientType);
        }

        public static VoteInfo GetVoteById(long voteId)
        {
            return new VoteDao().GetVoteById(voteId);
        }

        public static int GetVoteCounts(long voteId)
        {
            return new VoteDao().GetVoteCounts(voteId);
        }

        public static IList<VoteItemInfo> GetVoteItems(long voteId)
        {
            return new VoteDao().GetVoteItems(voteId);
        }

        public static DataSet GetVotes()
        {
            return new VoteDao().GetVotes(null);
        }

        public static bool InserBackInfo(string fileName, string version, long fileSize)
        {
            string filename = HiContext.Current.Context.Request.MapPath(Globals.ApplicationPath + "/config/BackupFiles.config");
            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                XmlNode node = document.SelectSingleNode("root");
                XmlElement newChild = document.CreateElement("backupfile");
                newChild.SetAttribute("BackupName", fileName);
                newChild.SetAttribute("Version", version.ToString());
                newChild.SetAttribute("FileSize", fileSize.ToString());
                newChild.SetAttribute("BackupTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                node.AppendChild(newChild);
                document.Save(filename);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool RestoreData(string bakFullName)
        {
            bool flag = new BackupRestoreDao().RestoreData(bakFullName);
            new BackupRestoreDao().Restor();
            return flag;
        }

        public static bool SaveMenu(MenuInfo menu)
        {
            return new MenuDao().SaveMenu(menu);
        }

        public static int SetVoteIsBackup(long voteId)
        {
            return new VoteDao().SetVoteIsBackup(voteId);
        }

        public static void SwapFriendlyLinkSequence(int linkId, int replaceLinkId, int displaySequence, int replaceDisplaySequence)
        {
            new FriendlyLinkDao().SwapFriendlyLinkSequence(linkId, replaceLinkId, displaySequence, replaceDisplaySequence);
        }

        public static void SwapHotWordsSequence(int hid, int replaceHid, int displaySequence, int replaceDisplaySequence)
        {
            new HotkeywordDao().SwapHotWordsSequence(hid, replaceHid, displaySequence, replaceDisplaySequence);
        }

        public static void SwapMenuSequence(int menuId, bool isUp)
        {
            new MenuDao().SwapMenuSequence(menuId, isUp);
        }

        public static bool UpdateFriendlyLink(FriendlyLinksInfo friendlyLink)
        {
            if (null == friendlyLink)
            {
                return false;
            }
            return new FriendlyLinkDao().CreateUpdateDeleteFriendlyLink(friendlyLink, DataProviderAction.Update);
        }

        public static void UpdateHotWords(int hid, int categoryId, string hotKeyWords)
        {
            new HotkeywordDao().UpdateHotWords(hid, categoryId, hotKeyWords);
        }

        public static bool UpdateMenu(MenuInfo menu)
        {
            return new MenuDao().UpdateMenu(menu);
        }

        public static bool UpdateVote(VoteInfo vote)
        {
            bool flag;
            using (DbConnection connection = DatabaseFactory.CreateDatabase().CreateConnection())
            {
                VoteDao dao = new VoteDao();
                connection.Open();
                DbTransaction dbTran = connection.BeginTransaction();
                try
                {
                    if (!dao.UpdateVote(vote, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    if (!dao.DeleteVoteItem(vote.VoteId, dbTran))
                    {
                        dbTran.Rollback();
                        return false;
                    }
                    int num = 0;
                    if (vote.VoteItems != null)
                    {
                        foreach (VoteItemInfo info in vote.VoteItems)
                        {
                            info.VoteId = vote.VoteId;
                            info.ItemCount = 0;
                            num += dao.CreateVoteItem(info, dbTran);
                        }
                        if (num < vote.VoteItems.Count)
                        {
                            dbTran.Rollback();
                            return false;
                        }
                    }
                    dbTran.Commit();
                    flag = true;
                }
                catch
                {
                    dbTran.Rollback();
                    flag = false;
                }
                finally
                {
                    connection.Close();
                }
            }
            return flag;
        }

        public static string UploadLinkImage(HttpPostedFile postedFile)
        {
            if (!ResourcesHelper.CheckPostedFile(postedFile))
            {
                return string.Empty;
            }
            string str = HiContext.Current.GetstoragePath() + "/link/" + ResourcesHelper.GenerateFilename(Path.GetExtension(postedFile.FileName));
            postedFile.SaveAs(HiContext.Current.Context.Request.MapPath(Globals.ApplicationPath + str));
            return str;
        }

        public static string UploadLogo(HttpPostedFile postedFile)
        {
            if (!ResourcesHelper.CheckPostedFile(postedFile))
            {
                return string.Empty;
            }
            string str = HiContext.Current.GetstoragePath() + ResourcesHelper.GenerateFilename(Path.GetExtension(postedFile.FileName));
            postedFile.SaveAs(HiContext.Current.Context.Request.MapPath(Globals.ApplicationPath + str));
            return str;
        }
    }
}

